import os
import urlparse

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from django.utils import simplejson

class RequestHandler(webapp.RequestHandler):
    def ok(self, data, ctype="application/json"):
        self.response.headers["Content-Type"] = ctype
        self.response.out.write(data)
    def json(self, data):
        cb = self.request.get("callback", None)
        if cb is not None:
            self.ok("%s(%s)" % (cb, simplejson.dumps(data)))
        else:
            self.ok(simplejson.dumps(data))
    def template(self, name, args):
        path = os.path.join(os.path.dirname(__file__), 'templates', name.lstrip('/'))
        self.response.out.write(template.render(path, args))
    def getint(self, name, default):
        try:
            return int(self.request.get(name, default))
        except:
            return int(default)

class NotFound(RequestHandler):
    def get(self, *args):
        return self.redirect("http://code.google.com/p/couchdb-bugs/")